/*
 * 文 件 名 : DolphinApplication.java
 * 版    权 : China Machi.Co.,Ltd.Copyright 2009-2011.All rights reserved
 * 描    述 : <描述>
 * 修 改 人 : lixg
 * 修改时间 : 2012-8-13
 * 跟踪单号 : <跟踪单号>
 * 修改单号 : <修改单号>
 * 修改内容 : <修改内容>
 */
package com.fire8.dolphin.ui;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.jivesoftware.smack.XMPPException;

import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.AsyncTask;
import android.os.Environment;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.widget.Toast;

import com.fire8.dolphin.model.UserInfo;
import com.fire8.dolphin.service.JaberClientService;
import com.fire8.dolphin.service.LbsService;
import com.fire8.dolphin.utils.LogUtil;
import com.fire8.dolphin.utils.MD5;
import com.fire8.dolphin.utils.StringUtil;
import com.fire8.dolphin.web.RequestUtils;
import com.fire8.dolphin.web.UserInfoWeb;

/**
 * <一句话功能简介><br>
 * <功能详细描述>
 *
 * @author  lixg
 * @version [版本号,2012-8-13]
 * @see     [相关类/方法]
 * @since   [产品/模块]
 */
public class DolphinApplication extends Application
{
    private final String TAG="dolphin";
    public static final String USER_KEY="a";//用户
    public static final String ACCOUNT_KEY="b";//账号
    public static final String PASSWORD_KEY="c";//密码
    public static final String FIRST_KEY="d";//是否第一次使用
    public static final String USER_ID="e";//用户ID
    public static final String MAC="f";//
    public static final String LOCATION="g";//用户所在地址
    public static final String TEL="h";//电话
    public static final String USER_NAME="user_name";
    public static final String USER_POINT="user_point";
    public static final String USER_TITLE="user_title";
    public static final String USER_REMARK="user_remark";
    public static final String USER_HEAD="user_head";
    private static final String ORDER_BY="orderby";
    private static final String USER_TYPE="user_type";//用户类别[0客户;1客服]
    
    public static final Integer PAGE_SIZE=50;//每页显示条数
    public static final Integer USER_TYPE_CLIENT=0;
    public static final Integer USER_TYPE_SERVER=1;
    
    public final static String IMG_PATH = Environment.getExternalStorageDirectory() + "/dolphin/"; 
    
    public final static String IP="www.aoao120.com";
    //public final static String IP="42.121.114.221";
    public final static String DOMAIN="dolphin_server";
    public final static String PORT="80";
    //public final static String URL="http://"+IP+"/";//生产环境
    public final static String URL="http://218.207.198.136:80/"+DOMAIN+"/";//测试环境
    
    private String account;
    private String password;
    
    private JaberClientService jaberClientService;
    private SharedPreferences sp;
    private Editor edit;
    //Edit edit;
    //检查是不是第一次使用
    //String first;
    
    @Override
    public void onCreate()
    {
        super.onCreate();
        LogUtil.d("onCreate DolphinApplication");
        sp = getSharedPreferences(USER_KEY, 0);
        
        edit = sp.edit();
        jaberClientService = JaberClientService.getInstance();
        
    }
    
    public void setUserInfo(UserInfo user)
    {
        if(!StringUtil.isEmpty(user.getUserName()))
        {
            edit.putString(USER_NAME, user.getUserName());
        }
        if(user.getPoint() != null)
        {
            edit.putLong(USER_POINT, user.getPoint());
        }
        if(!StringUtil.isEmpty(user.getUserTitle()))
        {
            edit.putString(USER_TITLE, user.getUserTitle());
        }
        if(!StringUtil.isEmpty(user.getRemark()))
        {
            edit.putString(USER_REMARK, user.getRemark());
        }
        if(user.getUserType() != null)
        {
            edit.putInt(USER_TYPE, user.getUserType());
        }
        edit.commit();
    }
    
    public String getUserId()
    {
        return sp.getString(USER_ID, "");
    }
    
    public String getTel()
    {
        return sp.getString(TEL, "");
    }
    public String getMac()
    {
        return sp.getString(MAC, "");
    }
    public String getLocation()
    {
        return sp.getString(LOCATION, "");
    }
    
    public void setUserId(String userId)
    {
        if(userId != null)
        {
            edit.putString(USER_ID, userId);
            //edit.putBoolean(FIRST_KEY, false);
            edit.commit();
            //Log.i(TAG,"写入userId到sp id="+sp.getString(USER_ID, ""));
        }
    }
    
    public String getAccount()
    {
        return sp.getString(ACCOUNT_KEY, "");
    }
    public String getPassword()
    {
        return sp.getString(PASSWORD_KEY, "");
    }
    
    public void setAccount(String acc,String pw)
    {
        if(!StringUtil.isEmpty(acc) && !StringUtil.isEmpty(pw))
        {
            edit.putString(ACCOUNT_KEY, acc);
            edit.putString(PASSWORD_KEY, pw);
            edit.commit();
        }
        
    }
    
    public String getUserName()
    {
        return sp.getString(USER_NAME, "");
    }
    public String getUserPoint()
    {
        return String.valueOf(sp.getLong(USER_POINT, 0));
    }
    public String getUserTitle()
    {
        return sp.getString(USER_TITLE, "");
    }
    public String getUserRemark()
    {
        return sp.getString(USER_REMARK, "");
    }
    public String getUserHead()
    {
        return sp.getString(USER_HEAD, "");
    }
    public String getOrderBy()
    {
        return sp.getString(ORDER_BY, "1");
    }
    //获得用户类型[0客户；1：客服]
    public int getUserType()
    {
        return sp.getInt(USER_TYPE, 0);
    }
    public void setOrderBy(String orderby)
    {
        edit.putString(ORDER_BY, orderby);
        edit.commit();
    }
    
    public void setUserHead(String strHead)
    {
        edit.putString(USER_HEAD, IMG_PATH+strHead);
        edit.commit();
    }
    
    public Bitmap getFriendHead(String userId) throws FileNotFoundException
    {
        return BitmapFactory.decodeStream(new FileInputStream(IMG_PATH+userId+".jpg"));
    }
    
    public boolean isFirst()
    {
        return sp.getBoolean(FIRST_KEY, true);
    }
    public void first(boolean flag)
    {
        edit.putBoolean(FIRST_KEY, flag);
        edit.commit();
    }
    
    /** 
     * 保存文件 
     * @param bm 
     * @param fileName 
     * @throws IOException 
     */  
    public void saveFile(Bitmap bm, String fileName){  
        File dirFile = new File(IMG_PATH);  
        if(!dirFile.exists()){  
            dirFile.mkdir();  
        }  
        
        try
        {
            File imgFile = new File(IMG_PATH + fileName+".jpg");  
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(imgFile));  
            bm.compress(Bitmap.CompressFormat.JPEG, 80, bos);  
            bos.flush();
            bos.close();  
            LogUtil.d("保存头像成功:"+IMG_PATH + fileName+".jpg");
        }
        catch (IOException e)
        {
            LogUtil.e("写入图像文件失败",e);
        }  
        
    }  
}
